package cn.Y_Qiu.pet.service.impl;

import cn.Y_Qiu.basic.service.impl.BaseServiceImpl;
import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.org.domain.Employee;
import cn.Y_Qiu.org.domain.Shop;
import cn.Y_Qiu.org.mapper.EmployeeMapper;
import cn.Y_Qiu.org.mapper.ShopMapper;
import cn.Y_Qiu.pet.domain.Pet;
import cn.Y_Qiu.pet.domain.SearchMasterMsg;
import cn.Y_Qiu.pet.mapper.PetMapper;
import cn.Y_Qiu.pet.mapper.SearchMasterMsgMapper;
import cn.Y_Qiu.pet.query.SearchMasterMsgQuery;
import cn.Y_Qiu.pet.service.ISearchMasterMsgService;
import cn.Y_Qiu.pet.util.DistanceUtil;
import cn.Y_Qiu.pet.util.Point;
import cn.Y_Qiu.user.domain.User;
import cn.Y_Qiu.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetServiceImpl petService;

    @Override
    public void searchpublish(SearchMasterMsg searchMasterMsg, Long id) {
        // 获取经纬点
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //获取最近商店
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.listAll());
        //1.2 把店铺绑定到消息
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        //1.3 用户绑定
        User user = userMapper.queryByLoginInfoId(id);
        searchMasterMsg.setUser(user);
        searchMasterMsg.setUser_id(user.getId());
        //1.4 对象消息进行持久化 此处给的mapper素材报bug
        searchMasterMsgMapper.add(searchMasterMsg);
        //2 发一个对象消息门店管理员
        Long admin_id = nearestShop.getAdmin_id();
        Employee employee = employeeMapper.queryById(admin_id);

        System.out.println(employee.getUsername() + "接收寻主请求");
        //SmsUtil.sendMsg(admin.getPhone(),"老板，
        // 来["+searchMasterMsg.getTitle()+"]生意了！赶快去接客！");
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        //查询已处理消息
        // state 1
        //shopid
        //userid
        Employee employee = employeeMapper.queryByLoginInfoId(id);
        // 有shopid是店铺人员，无则是平台人员
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        query.setState(1);
        return queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, Long id) {
        //个人中心，用户自己的消息
        // 根据Logininfoid获得自己的数据
        //shopid  无
        //userid
        User user = userMapper.queryByLoginInfoId(id);
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        // 查询待处理消息
        // state 0
        //shopid
        //userid
        Employee employee = employeeMapper.queryByLoginInfoId(id);
        // 有shopid是店铺人员，无则是平台人员
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        query.setState(0);
        PageList<SearchMasterMsg> searchMasterMsgPageList = queryPage(query);
        return queryPage(query);
    }

    @Override
    @Transactional
    public Ajaxresult handle(Pet pet, Long id) {
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());
        petService.add(pet);
        return Ajaxresult.me();
    }
//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
